package pro5_decoder_encoder.ex1_decoder;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;

// 自定义ChannelInitializer，用于初始化ChannelPipeline,并添加自定义的ChannelHandler.
public class MyLineBasedFrameDecoderChannelInitializer extends ChannelInitializer<SocketChannel> {

    private EventLoopGroup businessGroup;

    public MyLineBasedFrameDecoderChannelInitializer(EventLoopGroup businessGroup) {
        // 工作线程组
         this.businessGroup = businessGroup;
    }

    @Override
    protected void initChannel(SocketChannel channel) {
        channel.pipeline().addLast( "logging", new LoggingHandler(LogLevel.DEBUG)); // 增加一个日志处理的 handler

        //注意: 像 LineBasedFrameDecoder、StringDecoder 这样的解码器非常轻量，本质就是分隔符切分和字节转字符串，开销很小。
        //放在 I/O 线程 执行效率更高，避免多线程切换. 所以这里编解码器放 I/O 线程
        // 基于换行符号的解码器   ->　　Decoder完成切分消息.    byte[]　
        channel.pipeline().addLast(  new MyLineBasedFrameDecoder());
        // 解码  字节流转String
        channel.pipeline().addLast(  new StringDecoder());

        // 业务逻辑处理，可能耗时，放到businessGroup
        channel.pipeline().addLast(  this.businessGroup,  new MyLineBasedFrameDecoderServerHandler());

        //也可以指定线程组
       // channel.pipeline().addLast(  workThreadEventLoop,  new MyLineBasedFrameDecoderServerHandler());
    }
}
